package com.jxs.android.ges;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.serialization.SoapObject;

import android.annotation.SuppressLint;
import android.os.Message;
import android.text.SpannableString;

import com.jxs.android.weiget.BuLoadingDailog;
import com.jxs.android.weiget.ReportContainer;
import com.jxs.android.weiget.ReportContainer.Rate;
import com.jxs.android.weiget.TopBar;
import com.jxs.android.ges.R;

@SuppressLint("HandlerLeak")
public class TableList extends TableAbstractActivity {

	private ReportContainer mReport;
	private List<String> titles = new ArrayList<String>();
	private int scrollingStep = 1;
	private String selectedDate;

	@Override
	public int getLayout() {
		return R.layout.bu_tab_main;
	}

	@Override
	public void loadingData(int currentPage, boolean showDialog) {

		getWebServiceData(GesApplication.getUserInfo().getTicket(), Which.MY_ACHIVERMENT, showDialog, new DataInitListeren() {
			@Override
			public void initData(BuLoadingDailog progressDialog, boolean isShow) {
				progressDialog.dismiss();
			}
		}, PAGE_SIZE, currentPage);
	}

	class CstInfo {
		String cust_manager_name;// 客户经理
		String owe_00a_user_num;// 当月收入
		String owe_00d_user_num;// 上月收入
		String owe_00a_user_fee;// 上上月收入
		String owe_00d_user_fee;// 上上月收入
	}

	@Override
	public Object paserData(SoapObject retMsgObject, Object... params) {
		List<CstInfo> tems = new ArrayList<TableList.CstInfo>();
		if (null != retMsgObject) {
			for (int i = 0; i < retMsgObject.getPropertyCount(); i++) {
				SoapObject taskBeanObject = (SoapObject) retMsgObject.getProperty(i);
				CstInfo cstInfo = new CstInfo();
				cstInfo.cust_manager_name = taskBeanObject.getPropertySafelyAsString("cust_manager_name");// 客户类型
				cstInfo.owe_00a_user_num = taskBeanObject.getPropertySafelyAsString("owe_00a_user_num");// 客户ARPU
				cstInfo.owe_00d_user_num = taskBeanObject.getPropertySafelyAsString("owe_00d_user_num");// 产品信息
				cstInfo.owe_00a_user_fee = taskBeanObject.getPropertySafelyAsString("owe_00a_user_fee");// 收入信息
				cstInfo.owe_00d_user_fee = taskBeanObject.getPropertySafelyAsString("owe_00d_user_fee");// 收入信息
				tems.add(cstInfo);
			}
		}
		return tems;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void updateList(Message msg) {
		List<CstInfo> tems = (List<CstInfo>) msg.obj;
		if (null != tems && tems.size() > 0) {
			updateTableUI(tems);
		} else {
			errorMsgPop("暂无  " + selectedDate + "  数据！");
		}
	}

	public void initContent() {
		titles.clear();
		titles.add(getString(R.string.cust_manager_name));
		titles.add(getString(R.string.owe_00a_user_num));
		titles.add(getString(R.string.owe_00d_user_num));
		titles.add(getString(R.string.owe_00a_user_fee));
		titles.add(getString(R.string.owe_00d_user_fee));

		int index = titles.size() - 1;
		if (index == 4) {
			scrollingStep = 1;
		} else if (index > 4) {
			scrollingStep = 3;
		}
		mReport = (ReportContainer) findViewById(R.id.report_tab);
		fillEmptyData();
		if (titles.size() - 1 > 3) {
			mReport.initShoreMore(true);
		}
	}

	@Override
	public void topBarLogic(TopBar topBar) {
		super.topBarLogic(topBar);
		topBar.hindSpinnerBtn();
		topBarReturn(topBar);
		topBar.setTitle(title);
	}

	@Override
	public String getMethod() {
		return "getOweTotalPage";
	}

	private void fillEmptyData() {
		mReport.setTitleSize(ReportContainer.TITLE_SIZE_PS);
		mReport.initReportTitle(titles, false, this);
	}

	private void updateTableUI(List<CstInfo> tems) {

		for (CstInfo cstInfo : tems) {
			List<SpannableString> row = new ArrayList<SpannableString>();
			row.add(new SpannableString(cstInfo.cust_manager_name));
			row.add(new SpannableString(cstInfo.owe_00a_user_num));
			row.add(new SpannableString(cstInfo.owe_00d_user_num));
			row.add(new SpannableString(cstInfo.owe_00a_user_fee));
			row.add(new SpannableString(cstInfo.owe_00d_user_fee));
			mReport.addReportItem(row, scrollingStep, Rate.RATE_NONE, false);
		}

		int count = mReport.getRowNum();

		fillTable(count, titles.size());
	}

	private void fillTable(int rowCount, int titles) {
		if (rowCount < 16) {
			for (int index = rowCount; index < 16; index++) {
				List<SpannableString> row = new ArrayList<SpannableString>();
				for (int tabIndex = 0; tabIndex < titles; tabIndex++) {
					row.add(new SpannableString(""));
				}
				mReport.addReportItem(row, scrollingStep, Rate.RATE_NONE, false);
			}
		}
	}

}
